home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeed.internetmci.com!xmission!news
- From: macron@xmission.com (Joe Schlimgen)
- Newsgroups: comp.lang.c++
- Subject: Re: auto_ptr capability question
- Date: Wed, 03 Apr 1996 23:42:24 GMT
- Organization: XMission Internet (801 539 0900)
- Message-ID: <31630583.85660528@news.xmission.com>
- References: <31600aa3.1288774@news.xmission.com>
- NNTP-Posting-Host: slc138.xmission.com
- X-Newsreader: Forte Agent .99d/32.182
-
- On Mon, 01 Apr 1996 17:17:51 GMT (or thereabouts), willer@carolian.com
- (Steve Willer) wrote:
-
- [On my saying auto_ptr was part of the STL:]
-
- >First of all, auto_ptr is not part of the STL. It's part of the
- >standard C++ library. The "STL" encompasses the containers like list
- >and vector, iterators, and algorithms such as sort() and transform().
-
- True, but a minor detail that has nothing to do with the problem...
-
-
- [On an implicit conversion from auto_ptr<T> to T*:]
-
- >Now, as for explicit versus implicit...it's for the same reason that
- >std::string doesn't have an implicit conversion to "const char *":
- >It's dangerous, and bugs can creep in undetected. I'm perfectly happy
- >having to type a few more characters in order to avoid subtle bugs in
- >my code. For a detailed explanation, check Meyers's "More Effective
- >C++" book, item #5.
-
- I agree that a few extra keystrokes that avoid possible problems are
- well worth it. I've also been using my auto_ptr-like class and a string
- class that have implicit conversions with no problems (unless they're
- **very** subtle). Mostly, the implicit conversions have been used for
- parameters to older functions. Can you give any examples of the type of
- problems you're talking about?
-
- Thanks for the book reference, I'll check it out.
-
-
- [On the memory leak following assignment to an auto_ptr:]
-
- >Ownership is being _transferred_. Note that if you have something
- >like:
- >
- >auto_ptr<T> a;
- >auto_ptr<T> b(new T);
- >a = b;
- >
- >What this means is that at the second line, "b" owns a T, and "a" owns
- >nothing. When you say "a=b", "a" now owns the T and "b" owns nothing.
- >At the third line, no new T's are created -- the one T that got
- >explicitly created is simply passed around.
-
- I've always understood the transfer of ownership. Let me change your
- example just a bit to show the problem I'm talking about:
-
- auto_ptr<T> a(new T); // Given 'a' something to manage
- auto_ptr<T> b(new T);
- a = b;
-
- In line 1, 'a' is given the responsibility of managing a pointer to a
- 'T'. In line 3, 'b' is told to release it's ownership and transfer
- responsibility to 'a'. 'a' does *nothing* with the object it *was*
- managing and a leak occurs. In fact, the *only* way the object gets
- deleted is when the destructor is called. To fix this one must do the
- following:
-
- auto_ptr<T> a(new T);
- auto_ptr<T> b(new T);
- delete a.release(); // Delete the old T before getting new one
- a = b;
-
- (Talk about subtle bugs...) This places the responsibility of deleting
- the previous object on the programmer. And you know how responsible we
- are. :)
-
-
-
- -- The Truth Is Out There --
-